package com.study.d012Components.listview;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

/** 带拖拽效果的两个listview
*@description
*@author xtayhame
*date 2020年3月14日
*version
*since v1.0
*/
public class ListViewWithDrag2 extends Application{
	int index=0;   //记录当前要拖拽的cell索引
	String data="";  //记录当前要拖拽的cell的值

	int position=0;  //记录当前拖动到的位置

	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {

		ObservableList<String> datas=FXCollections.observableArrayList();
		datas.add("数据-A");
		datas.add("数据-B");
		datas.add("数据-C");
		datas.add("数据-D");
		datas.add("数据-E");

		ListView<String> listView = new ListView<String>(datas);
		listView.setPrefSize(200, 250);

		ListView<String> listView2 = new ListView<String>();
		listView2.setPrefSize(200, 250);



		AnchorPane anchorPane = new AnchorPane();
		anchorPane.getChildren().addAll(listView,listView2);
		AnchorPane.setTopAnchor(listView, 20.0);
		AnchorPane.setLeftAnchor(listView, 20.0);

		AnchorPane.setTopAnchor(listView2, 20.0);
		AnchorPane.setLeftAnchor(listView2, 240.0);

		stage.setScene(new Scene(anchorPane, 500, 400));
		stage.show();




		//第一步：获取当前点击的索引和值
		listView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				index = newValue.intValue();
				System.out.println("当前索引：" + index);
			}
		});

		listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				data = newValue;
				System.out.println("当前值：" + data);
			}
		});


		//第二步：
		listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
			@Override
			public ListCell<String> call(ListView<String> param) {
				Label label =new Label();
				ListCell cell = new ListCell<String>(){
					@Override
					protected void updateItem(String item, boolean empty) {
						super.updateItem(item, empty);
						if(!empty){
							label.setText(item);
							this.setGraphic(label);
						}
					}

				};

				//监测拖拽事件
				cell.setOnDragDetected(new EventHandler<MouseEvent>() {
					@Override
					public void handle(MouseEvent event) {
						//开始拖拽
						Dragboard db= cell.startDragAndDrop(TransferMode.COPY_OR_MOVE);

						System.out.println("截图：" +data);
						//Text tx = new Text(data);

						WritableImage wi = new WritableImage((int)cell.getWidth(), (int)cell.getHeight());
						//WritableImage wi = new WritableImage((int)param.getPrefWidth()-40,20);
						cell.snapshot(new SnapshotParameters(), wi);

						//截取图片
						db.setDragView(wi);
						//设置值
						ClipboardContent content = new ClipboardContent();
						content.putString(data);
						db.setContent(content);
					}
				});

				//拖动进入事件，为了获取拖动到的位置索引
				cell.setOnDragEntered(new EventHandler<DragEvent>() {

					@Override
					public void handle(DragEvent event) {
						position=param.getItems().indexOf(label.getText());
						System.out.println("拖拽到的索引位置：" + position  + "      值：" + label.getText());

						//后面的空值 返回的索引是-1
						if(position==-1){
							//如果是-1,默认就放在最后一个
							position = param.getItems().size() -1;
						}
						param.getFocusModel().focus(position);
					}
				});


				//拖动到目标上方时
				cell.setOnDragOver(new EventHandler<DragEvent>() {

					@Override
					public void handle(DragEvent event) {
						event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
					}
				});

				//拖动到目标松开时
				cell.setOnDragDropped(new EventHandler<DragEvent>() {
					@Override
					public void handle(DragEvent event) {
						//先删除
						System.out.println("删除的索引是：" + index);
						param.getItems().remove(index);
						//再添加   注意，这个值应该是从content中获取
						String newText=(String)event.getDragboard().getContent(DataFormat.PLAIN_TEXT);

						System.out.println("添加的索引：" + position + "    值:" + newText);
						param.getItems().add(position, newText);
					}
				});
				return cell;
			}
		});







		/****************************************************第二个ListView*************************************************/

		//拖动到目标上方时
		listView2.setOnDragOver(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
			}
		});

		//拖动到目标松开时
		listView2.setOnDragDropped(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				String newText=(String)event.getDragboard().getContent(DataFormat.PLAIN_TEXT);
				System.out.println("添加的索引：" + position + "    值:" + newText);
				listView2.getItems().add(newText);
			}
		});

	}

}
